package com.atguigu.w_threadPool;


import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/*如何自定义创建线程*/
public class Test02 {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                2, // 常驻核心线程池数
                5, // 线程池可以容纳的最大大小
                2L, // 多余空闲线程的存活时间
                TimeUnit.SECONDS, // 单位
                new LinkedBlockingQueue<Runnable>(3), // 长度为3的阻塞队列
                Executors.defaultThreadFactory(), // 使用默认工厂创建线程
                new ThreadPoolExecutor.DiscardPolicy()); // 线程的拒绝策略

        try {
            for (int i = 1; i <= 10; i++) {
                int finalI = i;
                threadPoolExecutor.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"线程在帮顾客"+ finalI +"" +
                            "处理业务");
                });
            }
        }finally {
            // 关闭
            threadPoolExecutor.shutdown();
        }
    }
}
